package com.jsy.imagepicker;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class PickerUtils {

    /**
     * 获取相册的所有相片
     *
     * @param context
     * @return
     */
    public static List<String> getImages(Context context,boolean filterGif) {
        List<String> list = new ArrayList<String>();
        ContentResolver contentResolver = context.getContentResolver();
        Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        String[] projection = {MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA,};
        String sortOrder = MediaStore.Images.Media.DATE_ADDED + " desc";
        Cursor cursor = contentResolver.query(uri, projection, null, null, sortOrder);
        int iId = cursor.getColumnIndex(MediaStore.Images.Media._ID);
        int iPath = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            String id = cursor.getString(iId);
            String path = cursor.getString(iPath);
            if (new File(path).exists()) {
                //Log.e("图片路径：", path + "");
                if (filterGif){
                    if (!isGifFile(new File(path))){
                        list.add(path);
                    }
                }else {
                    list.add(path);
                }
            }
            cursor.moveToNext();
        }
        cursor.close();
        return list;
    }

    /**
     * 是否是gif
     *
     * @param file
     * @return
     */
    public static boolean isGifFile(File file) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(file.getPath(), options);
        return "image/gif".equals(options.outMimeType);
    }

    /**
     * 获取图片保存文件夹路径
     * @param context
     * @return
     */
    public static String getImagesDirPath(Context context) {
        String path = Environment.getExternalStorageDirectory() + "/" + context.getPackageName() + "/"+"images/" ;
        mkdirs(path);
        return path;
    }


    /**
     * 在文件夹不存在时，创建
     *
     * @param path
     */
    static void mkdirs(String path) {
        try {
            if (!new File(path).exists()) {
                new File(path).mkdirs();
            }
        } catch (Exception e) {
        }
    }



    public static Uri getImageContentUri(Context context, File imageFile) {
        String filePath = imageFile.getAbsolutePath();
        Cursor cursor = context.getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                new String[]{MediaStore.Images.Media._ID},
                MediaStore.Images.Media.DATA + "=? ",
                new String[]{filePath}, null);

        if (cursor != null && cursor.moveToFirst()) {
            int id = cursor.getInt(cursor
                    .getColumnIndex(MediaStore.MediaColumns._ID));
            Uri baseUri = Uri.parse("content://media/external/images/media");
            return Uri.withAppendedPath(baseUri, "" + id);
        } else {
            if (imageFile.exists()) {
                ContentValues values = new ContentValues();
                values.put(MediaStore.Images.Media.DATA, filePath);
                return context.getContentResolver().insert(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
            } else {
                return null;
            }
        }
    }
}
